
package Staff;

import ConnnectData.DataConnect;
import Encript.Encripts;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author langtustupid
 **/
public class Change_pass1 extends javax.swing.JFrame {

    Connection Con;
    Statement Sta;
    ResultSet Res;
    PreparedStatement pst;
   
    public Change_pass1() {
        initComponents();
        this.setLocationRelativeTo(null);
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Change_pass1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Change_pass1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Change_pass1.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Change_pass1.class.getName()).log(Level.SEVERE, null, ex);
        }
        jLabel_ms.setText("");
        jLabel_msPass.setText("");
        jLabel_msPass2.setText("");
        jLabel_msPass3.setText("");
        
         try {
            Con = DataConnect.connectDB();
            Sta = Con.createStatement();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
       

       
    }
    String Username;

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPasswordField = new javax.swing.JPasswordField();
        jPasswordField2 = new javax.swing.JPasswordField();
        jPasswordField3 = new javax.swing.JPasswordField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel_msPass = new javax.swing.JLabel();
        jLabel_msPass3 = new javax.swing.JLabel();
        jLabel_msPass2 = new javax.swing.JLabel();
        jLabel_ms = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu_Update = new javax.swing.JMenu();
        jMenu_Close = new javax.swing.JMenu();

        jLabel5.setText("jLabel5");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Change password");

        jLabel2.setText("Current password :");

        jLabel3.setText("New password :");

        jLabel4.setText("Cofilm new password :");

        jPasswordField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jPasswordFieldFocusLost(evt);
            }
        });

        jPasswordField2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jPasswordField2FocusLost(evt);
            }
        });

        jPasswordField3.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jPasswordField3FocusLost(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 0, 0));
        jLabel6.setText("*");

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(255, 0, 0));
        jLabel7.setText("*");

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(255, 0, 0));
        jLabel8.setText("*");

        jLabel_msPass.setForeground(new java.awt.Color(255, 0, 0));
        jLabel_msPass.setText("Message");

        jLabel_msPass3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel_msPass3.setText("Message");

        jLabel_msPass2.setForeground(new java.awt.Color(255, 0, 0));
        jLabel_msPass2.setText("Message");

        jLabel_ms.setFont(new java.awt.Font("Tahoma", 0, 13)); // NOI18N
        jLabel_ms.setForeground(new java.awt.Color(255, 0, 0));
        jLabel_ms.setText("Message");

        jMenu_Update.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Untitled.jpg"))); // NOI18N
        jMenu_Update.setText(" Update ");
        jMenu_Update.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu_UpdateMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu_Update);

        jMenu_Close.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/exit.png"))); // NOI18N
        jMenu_Close.setText(" Close ");
        jMenu_Close.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu_CloseMouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu_Close);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPasswordField3, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                    .addComponent(jPasswordField2, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                    .addComponent(jPasswordField, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(15, 15, 15))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel_msPass2)
                    .addComponent(jLabel_msPass)
                    .addComponent(jLabel_msPass3))
                .addGap(120, 120, 120))
            .addGroup(layout.createSequentialGroup()
                .addGap(173, 173, 173)
                .addComponent(jLabel_ms)
                .addContainerGap(257, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_msPass)
                    .addComponent(jLabel6)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel_msPass2)
                    .addComponent(jLabel7)
                    .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_msPass3)
                    .addComponent(jLabel8)
                    .addComponent(jPasswordField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(28, 28, 28)
                .addComponent(jLabel_ms)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jPasswordFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordFieldFocusLost
       if(!jLabel_msPass.getText().equals(""))
       {
         jLabel_msPass.setText("");
       }
    }//GEN-LAST:event_jPasswordFieldFocusLost

    private void jPasswordField2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordField2FocusLost
       if(!jLabel_msPass2.getText().equals(""))
       {
         jLabel_msPass2.setText("");
       }
    }//GEN-LAST:event_jPasswordField2FocusLost

    private void jPasswordField3FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordField3FocusLost
       if(!jPasswordField3.getText().equals(""))
       {
         jLabel_msPass3.setText("");
       }
    }//GEN-LAST:event_jPasswordField3FocusLost

    private void jMenu_UpdateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu_UpdateMouseClicked

        if(jPasswordField.getText().equals(""))
        {
            jLabel_msPass.setText("Current pass not null !");
            
            jPasswordField.requestFocus();
            return;
        }
        if(jPasswordField.getText().length() > 15)
        {
            jLabel_msPass.setText("Current pass not validate !");
            jPasswordField.requestFocus();
            return;
        }
        if(jPasswordField2.getText().equals(""))
        {
            jLabel_msPass2.setText("new pass not null !");
            jPasswordField2.requestFocus();
            return;
        }
        if(jPasswordField2.getText().length() > 15)
        {
            jLabel_msPass2.setText("new pass must < 15 character !");
            jPasswordField2.requestFocus();
            return;
        }
        if(jPasswordField3.getText().equals(""))
        {
            jLabel_msPass3.setText("Cofilm pass not null !");
            jPasswordField3.requestFocus();
            
            return;
        }
        
        if(!jPasswordField3.getText().equals(jPasswordField2.getText().trim()))
        {
            jLabel_msPass3.setText("Password not the same !");
            System.out.println("Pass : "+jPasswordField3.getText());
            jPasswordField3.requestFocus();
            return;
        }
        
        System.out.println("Pass : "+jPasswordField.getText());        
        String pass = Encripts.encript(jPasswordField2.getText());
        String pass1 = Encripts.encript(jPasswordField.getText());
         try {
             
             Sta.executeQuery("Select * from Staff");
             pst = Con.prepareStatement("Update Staff set Staff_Password=? where Staff_Pemission = 'Inventory_Manager'");
             pst.setString(1, pass);
             pst.executeUpdate();
             this.dispose();
             JOptionPane.showMessageDialog(this, "Change successful password ");
             } catch (Exception e) {
                e.printStackTrace();
                }
    }//GEN-LAST:event_jMenu_UpdateMouseClicked

    private void jMenu_CloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu_CloseMouseClicked
        int Close=JOptionPane.showConfirmDialog(null, "Are you sure want close  !");
       if(Close==0)
       this.dispose();
    }//GEN-LAST:event_jMenu_CloseMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Change_pass1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel_ms;
    private javax.swing.JLabel jLabel_msPass;
    private javax.swing.JLabel jLabel_msPass2;
    private javax.swing.JLabel jLabel_msPass3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMenu_Close;
    private javax.swing.JMenu jMenu_Update;
    private javax.swing.JPasswordField jPasswordField;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JPasswordField jPasswordField3;
    // End of variables declaration//GEN-END:variables

}
